﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AppLayer;
using SpotItComponents;
using SpotItServerAPI;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for TwoPanelSelector.xaml
    /// </summary>
    public partial class TwoPanelSelector : UserControl
    {
        public Session session { get; set; }
        public Services services { get; set; }
        public SymbolSet SelectedSymbolSet { get; set; }
        public Category SelectedCategory { get; set; }
        public List<Category> categories = new List<Category>();
        public List<SymbolSet> sets = new List<SymbolSet>();
        public event EventHandler SymbolSetSelected;
        public event EventHandler SymbolSetUnselected;

        public TwoPanelSelector()
        {
            InitializeComponent();

            symbolSetDataGrid.CanUserResizeColumns = false;
            symbolSetDataGrid.SelectionMode = DataGridSelectionMode.Single;
            categoryDataGrid.CanUserResizeColumns = false;
            categoryDataGrid.SelectionMode = DataGridSelectionMode.Single;
            categoryDataGrid.CanUserAddRows = false;
            symbolSetDataGrid.CanUserAddRows = false;
        }

        public void loadGrids()
        {
            if(services != null && session != null)
            {
                OperationResult catResult = services.categoryManager.Lookup(session, null, null);
                OperationResult setResult = services.symbolManager.Lookup(session, null, null, null);
                if (catResult.Success)
                {
                    categories = catResult.Data as List<Category>;
                    
                }
                if(setResult.Success)
                {
                    sets = setResult.Data as List<SymbolSet>;
                }

                if (categoryDataGrid.SelectedItem != null)
                    SelectedCategory = (Category)categoryDataGrid.SelectedItem;
                categoryDataGrid.ItemsSource = categories;
                if (SelectedCategory != null)
                    filterSymbolSets();
                else
                    symbolSetDataGrid.ItemsSource = null;
            }
        }

        private void filterSymbolSets()
        {
            List<SymbolSet> selectedSymbolSets = new List<SymbolSet>();
            SelectedCategory = (Category)categoryDataGrid.SelectedItem;
            OperationResult result = services.symbolManager.Lookup(session, null, SelectedCategory, null);
            if (result.Success)
                selectedSymbolSets = result.Data as List<SymbolSet>;

            symbolSetDataGrid.ItemsSource = selectedSymbolSets;
        }

        private void categoryDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            filterSymbolSets();
        }

        private void symbolSetDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedSymbolSet = (SymbolSet)symbolSetDataGrid.SelectedItem;

            if (symbolSetDataGrid.SelectedItem != null)
            {
                if (SymbolSetSelected != null)
                    SymbolSetSelected(this, null);
            }
            else
                if (SymbolSetUnselected != null)
                    SymbolSetUnselected(this, null);
                
        }

        public void deleteSelectedCategory()
        {
            if (SelectedCategory != null)
            {
                services.categoryManager.Remove(session, SelectedCategory);
                SelectedCategory = null;
                loadGrids();
            }
        }

        public void deleteSelectedSymbolSet()
        {
            if (SelectedSymbolSet != null)
            {
                services.symbolManager.Remove(session, SelectedSymbolSet);
                SelectedSymbolSet = null;
                loadGrids();
            }
        }

        public void addNewCategory(string categoryLabel)
        {
            SelectedCategory = null;
            SelectedSymbolSet = null;

            Category newCategory = new Category(categoryLabel);
            services.categoryManager.Add(session, newCategory);

            loadGrids();
        }

        public void addNewSymbolSet(string symbolSetLabel)
        {
            SelectedSymbolSet = null;

            SymbolSet newSymbolSet = new SymbolSet(symbolSetLabel, false);
            if (SelectedCategory != null)
                newSymbolSet.AddCategory(SelectedCategory);

            services.symbolManager.Add(session, newSymbolSet);

            loadGrids();
        }

        //private void tb1OnKeyDownHandler(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Return)
        //    {
        //        string text = textBox1.Text;
        //        Category addCategory = new Category(text);
        //        services.categoryManager.Add(session, addCategory);
        //        //update ui
        //        categories.Add(addCategory);

        //    }

        //}


        //private void tb2OnKeyDownHandler(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Return)
        //    {
        //        string text = textBox2.Text;
        //        SymbolSet addSymbolSet = new SymbolSet(text, false);
        //        services.symbolManager.Add(session, addSymbolSet);
        //        //update ui
        //        sets.Add(addSymbolSet);
        //    }
        //}
    }
}
